// JavaScript Document

(function( $ ){

  $.fn.wsnFader = function(options) {
  
        var defaults = {   
				interval: 5000,
				speed: 800,
				slwidth: $(this).width(), 
		  };

		var settings = $.extend({}, defaults, options);  
		
		if (settings.interval <= settings.speed) {
			settings.speed = settings.interval - 100;	
		}
		
		this.each(function() {
				   
			var $this = $(this); //store reference	
			$('.buttons').css({'visibility':"visible"});
			$('.caption').hide();
			$('.caption').css({'opacity':"0.7"});
			
			$('.buttons li:first').addClass("current");
			var imgSrc = $('.buttons li.current a').attr("href");
			$('.buttons li a').each (function (i){
					 $this.append("<img src='" + $(this).attr("href") + "' class='buffer' />");
					 $(this).attr("alt", $(this).text()).text(i+1);
											   });
			$this.prepend("<img src='" + imgSrc + "'/>");
			$($this).find('img').not('.buffer').css({ position:"absolute", top:0, left:0 });
			$('.caption').slideDown(500);
			rotator = setInterval(function() {nextslide($this, settings)}, settings.interval);
  		
			$('.buttons li a').click(function(evt) {
					evt.preventDefault();
					clearInterval(rotator);
					var imgSrc = $(this).attr("href");
					var title = $(this).attr("title");
					var alt = $(this).attr("alt");
					$($this).find('img').eq(1).attr("src", imgSrc).show(0);
					$('.caption').slideUp(100, function () {
						$($this).find('img').eq(0).fadeOut(100, function() {
								$($this).find('img').eq(0).attr("src", imgSrc).show(0);
								$('.caption h3').text(alt);
								$('.caption p').html(title);
								$('.caption').slideDown(500);	
						});
					});
					$('.buttons li.current').removeClass("current");
					$(this).parent().addClass("current");
					rotator = setInterval(function() {nextslide($this, settings)}, settings.interval);
			});			
		
		});					 
		
		return this;	

	};

		nextslide = function ($this, settings) {
					$($this).find('img').eq(1).css({opacity:"0.0"});
					var nextImage = $('.buttons li.current').next();
					if (nextImage.length == 0) {
						$('.buttons li.current').removeClass("current").siblings(":first").addClass("current");
					} else {	
						$('.buttons li.current').removeClass("current").next().addClass("current");
					}
						var imgSrc = $('.buttons li.current a').attr("href");
						var title = $('.buttons li.current a').attr("title");
						var alt = $('.buttons li.current a').attr("alt");
						$($this).find('img').eq(1).attr("src", imgSrc).animate({opacity:"1.0"},(settings.speed));
						$('.caption').slideUp(100, function () {
						$($this).find('img').eq(0).animate({opacity:"0.0"}, settings.speed, function() {
																					$(this).attr("src", imgSrc).css({opacity:"1.0"});
																					$('.caption h3').text(alt);
																					$('.caption p').html(title);
																					$('.caption').slideDown(500);
																					
																								});	
						});
		};


})( jQuery );


$('document').ready(function () {
		$('#wsnFader').wsnFader({interval:6000, speed:300});
});